using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace ZSW.Framework.UIElement
{
    [RequireComponent(typeof(Slider))]
    public class ZSWF_Sliderbar : ZSWF_BaseUIElement
    {
        public Action<float> OnConfirm;
        public Action<float> OnValueChange;
        [SerializeField] private Text m_fillAmountText;
        [SerializeField] private Slider m_sliderBar;
        private Coroutine m_coroutine;
        public override void Init()
        {
            m_sliderBar = gameObject.GetComponent<Slider>();
            m_sliderBar.onValueChanged.AddListener((float value) =>
            {
                if (m_fillAmountText != null)
                {
                    m_fillAmountText.text = (value * 100).ToString("0") + "%";
                }
                OnValueChange?.Invoke(value);
            });

            addEventTrigger();
            base.Init();
        }

        private void addEventTrigger()
        {
            //在当前组件下添加EventTrigger组件,event组件添加Pointer up事件，pointer up事件添加OnPointerUp方法，OnPointerUp方法调用OnConfirm方法
            var eventTrigger = gameObject.AddComponent<EventTrigger>();
            eventTrigger.triggers = new List<EventTrigger.Entry>();
            var pointerUp = new EventTrigger.Entry();
            pointerUp.eventID = EventTriggerType.PointerUp;
            pointerUp.callback.AddListener((data) =>
            {
                OnConfirm?.Invoke(m_sliderBar.value);
            });
            eventTrigger.triggers.Add(pointerUp);
        }


        private void OnDisable()
        {
            if (m_coroutine != null)
            {
                StopCoroutine(m_coroutine);
            }
        }

        public void SetNA()
        {
            m_sliderBar.value = 0;
            m_fillAmountText.text = "无";
        }

        public void SetValue(float value)
        {
            if (m_coroutine != null)
            {
                StopCoroutine(m_coroutine);
            }
            m_sliderBar.value = value;

            m_fillAmountText.text = $"{Mathf.RoundToInt(value * 100)}%";
        }

        public void SliderValue(float value)
        {
            if (m_coroutine != null)
            {
                StopCoroutine(m_coroutine);
            }
            if (gameObject.activeInHierarchy)
            {
                m_coroutine = StartCoroutine(AnimateSlider(value));
            }
            else
            {
                SetValue(value);
            }
        }

        private IEnumerator AnimateSlider(float value)
        {
            float time = 0;
            float startValue = m_sliderBar.value;
            float endValue = value;
            while (time < 1)
            {
                time += UnityEngine.Time.deltaTime;
                float tmpValue = Mathf.Lerp(startValue, endValue, time);
                m_sliderBar.value = tmpValue;
                m_fillAmountText.text = $"{Mathf.RoundToInt(tmpValue * 100)}%";
                yield return null;
            }
        }

    }
}